﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Web.UI;
using DevExpress.Web.ASPxHeadline;
using DevExpress.Web.ASPxClasses;
using DevExpress.Web.ASPxCallback;
using DevExpress.Web.ASPxGridView;

using LMD.Constant;
using LMD.Lib.DataClass;
using LMD.Lib.Interface;

public partial class CARAvailability : ICPRelationshipPage
{
    public DateTime m_dtmStartDate;
    public DateTime m_dtmEndDate;
    public DataTable m_dtCAR;


    #region Properties
    
    
    #endregion Properties

    #region Event Handlers

    protected override void Page_Load(object sender, EventArgs e)
    {
        base.Page_Load(sender, e);

        Page.Title = "ICP Lintasarta: Relationship, CAR Availability";
        if (!IsPostBack && !IsCallback)
        {
            Session[KSession.CA_STARTDATE] = null;
            Session[KSession.CA_CUSTOMERID] = null;
            Session[KSession.CA_CARID] = null;
        }
        InitAvailability();    
    }
    protected void dgCA_CustomCallback(object sender, ASPxGridViewCustomCallbackEventArgs e)
    {
        if (e.Parameters.ToUpper().StartsWith("FILTER"))
        {
            HandleFilter(e.Parameters);
        }
        else if (e.Parameters.ToUpper().StartsWith("RELOAD"))
        {
            HandleReload(e.Parameters);
        }
    }
    protected void cbkCA_Callback(object sender, CallbackEventArgs e)
    {
        System.Threading.Thread.Sleep(KGlobal.AJAX_WAIT_TIME);
        if (e.Parameter.ToUpper().StartsWith("SHOW"))
        {
            e.Result = HandleShowPopUp(e.Parameter);
        }
    }
    #endregion Event Handlers

    #region Custom Methods
    protected override void DoSearch(String sxSearchQuery)
    {
        throw new NotImplementedException();
    }
    protected void InitAvailability()
    {
        DateTime dtmDate = 
            Session[KSession.CA_STARTDATE] != null ? 
            Convert.ToDateTime(Session[KSession.CA_STARTDATE]) : 
            DateTime.Now;
        int iCustomerId =
            Session[KSession.CA_CUSTOMERID] != null ?
            Convert.ToInt32(Session[KSession.CA_CUSTOMERID]) : 
            -1;
        int iCarId =
            Session[KSession.CA_CARID] != null ?
            Convert.ToInt32(Session[KSession.CA_CARID]) :
            -1;
        GetStartEndDate(dtmDate);
        Cached.CARSpocTable cachedCarSpoc = new Cached.CARSpocTable();
        m_dtCAR = cachedCarSpoc.GetCARAvailability(m_dtmStartDate,iCustomerId, iCarId);
        GetDateCaption();

        dgCA.DataSource = m_dtCAR;
        dgCA.DataBind();
    }
    protected String HandleFilter(String sxParameter)
    {
        String[] sxSplitted = sxParameter.Split(new String[] { KGlobal.DELIMITER }, StringSplitOptions.None);
        String sxReturn = String.Empty;
        int iCompanyId = -1;
        int iCARId = -1;
        if (!String.IsNullOrEmpty(sxSplitted[1]) && !String.IsNullOrEmpty(sxSplitted[2]))
        {
            iCompanyId = Convert.ToInt32(sxSplitted[1]);
            iCARId = Convert.ToInt32(sxSplitted[2]);
        }
        else if (!String.IsNullOrEmpty(sxSplitted[1]) && String.IsNullOrEmpty(sxSplitted[2]))
        {
            iCompanyId = Convert.ToInt32(sxSplitted[1]);
        }
        else if(!String.IsNullOrEmpty(sxSplitted[2]))
        {
            iCARId = Convert.ToInt32(sxSplitted[2]);
        }
        Session[KSession.CA_STARTDATE] = Convert.ToDateTime(sxSplitted[3]);
        Session[KSession.CA_CUSTOMERID] = iCompanyId;
        Session[KSession.CA_CARID] = iCARId;
        GetDateCaption();
        InitAvailability();
        return sxReturn;
    }
    protected String HandleReload(String sxParameter)
    {
        String[] sxSplitted = sxParameter.Split(new String[] { KGlobal.DELIMITER }, StringSplitOptions.None);
        String sxReturn = String.Empty;
        String sxType = sxSplitted[1];
        int iCompanyId = -1;
        int iCARId = -1;
        if (!String.IsNullOrEmpty(sxSplitted[2]) && !String.IsNullOrEmpty(sxSplitted[3]))
        {
            iCompanyId = Convert.ToInt32(sxSplitted[2]);
            iCARId = Convert.ToInt32(sxSplitted[3]);
        }
        else if (!String.IsNullOrEmpty(sxSplitted[2]) && String.IsNullOrEmpty(sxSplitted[3]))
        {
            iCompanyId = Convert.ToInt32(sxSplitted[2]);
        }
        else if (!String.IsNullOrEmpty(sxSplitted[3]))
        {
            iCARId = Convert.ToInt32(sxSplitted[3]);
        }
        Session[KSession.CA_STARTDATE] = Convert.ToDateTime(sxSplitted[4]);
        Session[KSession.CA_CUSTOMERID] = iCompanyId;
        Session[KSession.CA_CARID] = iCARId;
        GetDateCaption();
        InitAvailability();
        return sxReturn;
    }
    protected String HandleShowPopUp(String sxParameter)
    {
        String sxOutput = String.Empty;
        String[] sxSplitted = sxParameter.Split(new String[] { KGlobal.DELIMITER }, StringSplitOptions.None);
        String sxCarId = sxSplitted[1];
        String sxDay = sxSplitted[2];
        DateTime dtmStartDate = Convert.ToDateTime(sxSplitted[3]);
        int x = 0;
        switch (sxDay)
        {
            case "Monday":
                x = 0;
                break;
            case "Tuesday":
                x = 1;
                break;
            case "Wednesday":
                x = 2;
                break;
            case "Thursday":
                x = 3;
                break;
            case "Friday":
                x = 4;
                break;
        }
        DateTime dtmDate = dtmStartDate.AddDays(x);
        dtmDate = dtmDate.AddHours(-(dtmDate.Hour));
        dtmDate = dtmDate.AddMinutes(-(dtmDate.Minute));
        dtmDate = dtmDate.AddSeconds(-(dtmDate.Second));
        dtmDate = dtmDate.AddMilliseconds(-(dtmDate.Millisecond));
        DataTable dt = DBQuery.CustomerVisitTable.GetVisitByCarAndDate(sxCarId, dtmDate);
        sxOutput = CARManager.BuildEditor_PopUp("PopUp", dt);
        return sxOutput;
    }
    
    protected void GetStartEndDate(DateTime dtmDate)
    {
        String sxDayOfWeek = dtmDate.DayOfWeek.ToString();
        switch (sxDayOfWeek.ToUpper())
        {
            case "SUNDAY":
                m_dtmStartDate = dtmDate.AddDays(1);
                m_dtmEndDate = dtmDate.AddDays(5);
                break;
            case "MONDAY":
                m_dtmStartDate = dtmDate;
                m_dtmEndDate = dtmDate.AddDays(4);
                break;
            case "TUESDAY":
                m_dtmStartDate = dtmDate.AddDays(-1);
                m_dtmEndDate = dtmDate.AddDays(3);
                break;
            case "WEDNESDAY":
                m_dtmStartDate = dtmDate.AddDays(-2);
                m_dtmEndDate = dtmDate.AddDays(2);
                break;
            case "THURSDAY":
                m_dtmStartDate = dtmDate.AddDays(-3);
                m_dtmEndDate = dtmDate.AddDays(1);
                break;
            case "FRIDAY":
                m_dtmStartDate = dtmDate.AddDays(-4);
                m_dtmEndDate = dtmDate;
                break;
            case "SATURDAY":
                m_dtmStartDate = dtmDate.AddDays(2);
                m_dtmEndDate = dtmDate.AddDays(6);
                break;
        }
    }
    protected String GetDayColumn(object objDay, object objCarId, String sxDay)
    {
        if (!objDay.Equals(DBNull.Value))
        {
            int iDay = Convert.ToInt32(objDay);
            if (iDay > 0)
            {
                return String.Format(
                    "<img src=\"images/close.png\" onclick=\"javascript:ShowPopUp('{0}','{1}');\"  style=\"cursor:pointer\" />",
                    objCarId.ToString(), sxDay);
            }
            else
            {
                return String.Format("<img src=\"images/check.png\" />");
            }
        }
        else
        {
            return String.Format("<img src=\"images/check.png\" />");
        }
    }
    protected void GetDateCaption()
    {
        String[] sxDays = { "MON", "TUE", "WED", "THU", "FRI" };
        DateTime dtmStartDate = m_dtmStartDate;
        String sxDate = String.Empty;
        foreach (String sxDay in sxDays)
        {
            switch (sxDay.ToUpper())
            {
                case "MON":
                    sxDate = dtmStartDate.ToString("dd/MM");
                    break;
                case "TUE":
                    sxDate = dtmStartDate.AddDays(1).ToString("dd/MM");
                    break;
                case "WED":
                    sxDate = dtmStartDate.AddDays(2).ToString("dd/MM");
                    break;
                case "THU":
                    sxDate = dtmStartDate.AddDays(3).ToString("dd/MM");
                    break;
                case "FRI":
                    sxDate = dtmStartDate.AddDays(4).ToString("dd/MM");
                    break;
            }
            dgCA.Columns[sxDay].Caption = sxDate;
        }
    }
    #endregion Custom Methods
}
